package com.sgb.goods.api;

import com.sgb.goods.BuildConfig;
import com.sgb.goods.utils.StringUtils;
import com.sgb.net.core.BaseCompatibleNet;
import com.swgk.core.util.MLog;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * 描述： 上传文件网络请求类
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021/7/6 09:36
 */
public class UploadNet extends BaseCompatibleNet {

    private final FileUploadApi mNewUploadService;

    private UploadNet() {
        mNewUploadService = getService(FileUploadApi.class, BuildConfig.BASE_URL_SP);
    }

    private static class Single {
        private static final UploadNet INSTANCE = new UploadNet();
    }

    public static UploadNet getInstance() {
        return Single.INSTANCE;
    }

    @Override
    protected void handleBuilder(OkHttpClient.Builder builder) {
        builder.connectTimeout(150, TimeUnit.SECONDS)
                .readTimeout(150, TimeUnit.SECONDS)
                .writeTimeout(150, TimeUnit.SECONDS);

        if (BuildConfig.IS_MODULE_DEBUG) {
            builder.addInterceptor(chain -> {
                String token = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.resourcePCToken, "");
                MLog.d("TAG", "Bearer " + token);
                return chain.proceed(chain.request().newBuilder()
                        .addHeader("Authorization", StringUtils.isEmpty(token) ? "Basic Z2N0eDpnY3R4" : String.format("Bearer %s", token))
                        .addHeader("platform", "android")
                        .addHeader("Content-Type", "application/x-www-form-urlencoded")
                        .addHeader("type", "app-api-java")
                        .build());
            });
        }
    }


    public FileUploadApi getFileUploadApi() {
        return mNewUploadService;
    }
}
